package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhijian.medical.dao.local.RoutineSingleSampleMapper;
import com.zhijian.medical.dao.local.RoutineSingleSampleQpMapper;
import com.zhijian.medical.entity.po.RoutinePatient;
import com.zhijian.medical.entity.po.RoutineSingleSample;
import com.zhijian.medical.entity.po.RoutineSingleSampleQp;
import com.zhijian.medical.entity.vo.request.RoutineSampleSearchVo;
import com.zhijian.medical.entity.vo.request.RoutineSingleSampleSearchVo;
import com.zhijian.medical.entity.vo.request.RoutineSingleSampleSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.RoutineSampleVo;
import com.zhijian.medical.entity.vo.response.RoutineSingleSampleVo;
import com.zhijian.medical.service.RoutinePatientService;
import com.zhijian.medical.service.RoutineSampleService;
import com.zhijian.medical.service.RoutineSingleSampleService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 常规病例-取材-取材明细业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-07-24 19:22:00
 */
@Slf4j
@Service
public class RoutineSingleSampleServiceImpl implements RoutineSingleSampleService {

	@Autowired
    private RoutineSingleSampleMapper mapper;
    @Autowired
    private RoutineSampleService sampleService;
    @Autowired
    private RoutinePatientService patientService;

    @Autowired
    private RoutineSingleSampleQpMapper routineSingleSampleQpMapper;

    @Override
    public RoutineSingleSample selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutineSingleSampleVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutineSingleSample routineSingleSample) {
        Date now = new Date();
        routineSingleSample.setCreateTime(now);
        routineSingleSample.setUpdateTime(now);
        return mapper.insert(routineSingleSample) > 0 ? routineSingleSample.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutineSingleSample routineSingleSample) {
        Date now = new Date();
        routineSingleSample.setCreateTime(now);
        routineSingleSample.setUpdateTime(now);
        return mapper.insertSelective(routineSingleSample) > 0 ? routineSingleSample.getId() : 0L;
    }

    @Override
    public boolean update(RoutineSingleSample routineSingleSample) {
        Date now = new Date();
        routineSingleSample.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routineSingleSample) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutineSingleSample record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutineSingleSampleSaveOrUpdateVo saveOrUpdateVo) {
        RoutineSingleSample routineSingleSample = new RoutineSingleSample();
        BeanUtils.copyProperties(saveOrUpdateVo, routineSingleSample);
        return this.insert(routineSingleSample) != 0;
    }

    @Override
    public boolean modify(RoutineSingleSampleSaveOrUpdateVo saveOrUpdateVo) {
        RoutineSingleSample old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutineSingleSample routineSingleSample = new RoutineSingleSample();
        BeanUtils.copyProperties(saveOrUpdateVo, routineSingleSample);
        return this.update(routineSingleSample);
    }

    @Override
    public RoutineSingleSampleVo selectOne(RoutineSingleSampleSearchVo routineSingleSampleSearchVo) {
        return mapper.selectOne(routineSingleSampleSearchVo);
     }

    @Override
    public Integer count(RoutineSingleSampleSearchVo routineSingleSampleSearchVo) {
        return mapper.count(routineSingleSampleSearchVo);
    }

    @Override
    public List<RoutineSingleSampleVo> selectList(RoutineSingleSampleSearchVo routineSingleSampleSearchVo) {
        List<RoutineSingleSampleVo> routineSingleSampleVos = mapper.selectList(routineSingleSampleSearchVo);
        for (RoutineSingleSampleVo vo: routineSingleSampleVos) {
            String[] split = vo.getNumber().split("-");
            if (split.length == 2) {
                // 找到病理号
                // 先获取所有的大样本信息
                List<RoutineSampleVo> routineSampleVos = sampleService.selectList(RoutineSampleSearchVo.builder().conventionPathologyNumber(split[0]).build());
                if (CollUtil.isEmpty(routineSampleVos)) {
                    continue;
                }
                // 病人信息
                RoutineSampleVo routineSampleVo = routineSampleVos.get(0);
                RoutinePatient routinePatient = patientService.selectById(routineSampleVo.getPatientId());
                vo.setPatientName(routinePatient.getName());
            }
        }
        return routineSingleSampleVos;
    }

    @Override
    public List<RoutineSingleSampleQp> selectQpList(RoutineSingleSampleSearchVo routineSingleSampleSearchVo){
        List<RoutineSingleSampleVo> routineSingleSampleVos = mapper.selectList(routineSingleSampleSearchVo);
        List<Long> sampleSingleId = routineSingleSampleVos.stream().map(RoutineSingleSampleVo::getId).collect(Collectors.toList());
        QueryWrapper<RoutineSingleSampleQp> queryWrapper = new QueryWrapper<>();
        List<RoutineSingleSampleQp> dataList = new LinkedList<>();
        if (CollectionUtil.isNotEmpty(sampleSingleId)){
            queryWrapper.in("sample_id", sampleSingleId);
            dataList = routineSingleSampleQpMapper.selectList(queryWrapper);
        }
        Map<String, String> patientInfo = new HashMap<>();
        for (RoutineSingleSampleVo vo: routineSingleSampleVos) {
            String[] split = vo.getNumber().split("-");
            if (split.length == 2) {
                // 找到病理号
                // 先获取所有的大样本信息
                List<RoutineSampleVo> routineSampleVos = sampleService.selectList(RoutineSampleSearchVo.builder().conventionPathologyNumber(split[0]).build());
                if (CollUtil.isEmpty(routineSampleVos)) {
                    continue;
                }
                // 病人信息
                RoutineSampleVo routineSampleVo = routineSampleVos.get(0);
                RoutinePatient routinePatient = patientService.selectById(routineSampleVo.getPatientId());
                //vo.setPatientName(routinePatient.getName());
                patientInfo.put(split[0], routinePatient.getName());
            }
        }
        for (RoutineSingleSampleQp routineSingleSampleQp : dataList){
            String[] split = routineSingleSampleQp.getNumber().split("-");
            String blh = split[0];
            routineSingleSampleQp.setPatientName(patientInfo.get(blh));
        }
        return dataList;
    }

    @Override
    public Page<RoutineSingleSampleVo> selectPage(RoutineSingleSampleSearchVo routineSingleSampleSearchVo) {
        AssertUtil.check(Objects.isNull(routineSingleSampleSearchVo.getPageNum()) || Objects.isNull(routineSingleSampleSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routineSingleSampleSearchVo);
        //分页
        Page<RoutineSingleSampleVo> page = new Page<>(true, count, routineSingleSampleSearchVo.getPageNum(), routineSingleSampleSearchVo.getPageSize());
        routineSingleSampleSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutineSingleSampleVo> responseVos = this.selectList(routineSingleSampleSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutineSingleSample> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutineSingleSampleSearchVo routineSingleSampleSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routineSingleSampleSearchVo);
    }

    @Override
    public List<String> selectNumList(String number) {
        return mapper.selectNumList(number);
    }
    
}